

clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user

set graphics on  
 
global path_data_processed "\data\processed"
global path_output "\data\output"

 
clear all
set more off

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
 

 
loc experiments "z_ndcons_index_A z_D_log_spending_f z_durables durablepurchase z_stocktrade_f stocks_incr_f stocks_decr_f"


replace D_log_spending_f=. if D_log_spending_f!=D_log_spending_f_w02
replace z_D_log_spending_f=. if D_log_spending_f!=D_log_spending_f_w02


foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1
global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

preserve

clear all
eststo clear
estimates drop _all

set obs 10
qui gen x = 1
qui gen y = 1

loc columns = 0

foreach choice in `experiments' {

    loc ++columns
    qui eststo col`columns': reg x y

}

restore


/* Statistics */

loc colnum = 1
loc colnames ""
 

foreach choice in `experiments' {


    qui ivreg2 `choice' (updating = shock) prior $controls , r first
    mat firststage = e(first)
	mat r2=e(r2)
	sigstar updating, prec(3)
    estadd loc thisstat2 = "`r(bstar)'": col`colnum'
    estadd loc thisstat3 = "`r(sestar)'": col`colnum'
	sigstar prior, prec(3)
    estadd loc thisstat5 = "`r(bstar)'": col`colnum'
    estadd loc thisstat6 = "`r(sestar)'": col`colnum'
	qui sum `choice'
    estadd loc thisstat8 = r(N): col`colnum'	
	*estadd loc thisstat9 = string(r2[1,1], "%9.2f"): col`colnum'	
	estadd loc thisstat10 = string(r(mean), "%9.2f"): col`colnum'
	estadd loc thisstat11 = string(r(sd), "%9.2f"): col`colnum'	
	estadd loc thisstat12 = string(firststage[8,1], "%9.2f"): col`colnum'	
		
    loc ++colnum
    loc colnames "`colnames' `"`: var la `choice''"'"

}
	 
	
	
loc rowlabels " " " "Updating: Recession" " " " " "Prior" " " " " "Observations" " " "Mean dep. var." "SD dep. var." "First stage F-stat" " "   "\hline" " "   "
loc rowstats ""

forval i = 1/12 {
    loc rowstats "`rowstats' thisstat`i'"
}

esttab * using "table_behavior.tex", replace cells(none) booktabs nonotes nomtitles compress alignment(c) nogap noobs nobaselevels label stats(`rowstats', labels(`rowlabels')) ///
    mgroups("\shortstack{Consumption\\growth\\(planned)}" "\shortstack{Consumption\\growth\\(actual)}" ///
  "\shortstack{Durable\\purchase\\climate}" "\shortstack{Durable\\purchase\\(actual)}" "\shortstack{Stocks net\\purchases\\(scale)}" ///
  "\shortstack{Stocks net\\purchases\\(dummy)}" "\shortstack{Stocks net\\sales\\(dummy)}", pattern(1 1 1 1 1 1 1 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

eststo clear	

